package com.shuigu.controller;


import com.shuigu.response.PageResult;
import com.shuigu.response.ShuiGuResult;
import com.shuigu.pojo.dto.UmsMenuParamDto;
import com.shuigu.pojo.entity.UmsMenu;
import com.shuigu.pojo.vo.RouterVO;
import com.shuigu.service.IUmsMenuService;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("sys/menu")
public class MenuController {

    private final IUmsMenuService menuService;

    public MenuController(IUmsMenuService menuService) {
        this.menuService = menuService;
    }

    @PostMapping
    public ShuiGuResult saveMenu(@Valid @RequestBody UmsMenu umsMenu) {
        int row = menuService.saveMenu(umsMenu);
        if(row > 0) {
            return ShuiGuResult.success();
        }
        return ShuiGuResult.error("新增菜单失败");
    }

    @PutMapping
    public ShuiGuResult updateMenu(@Valid @RequestBody UmsMenu umsMenu) {
        int row = menuService.updateMenu(umsMenu);
        if(row > 0) {
            return ShuiGuResult.success();
        }
        return ShuiGuResult.error("修改菜单失败");
    }

    /**
     * 查询自己的菜单
     */
    @GetMapping("self")
    public ShuiGuResult searchSelfMenu() {
        // 获取当前登录的用户id,都在SecurityContextHolder中存储
        List<RouterVO> routerVOList = menuService.searchSelfMenu();
        return ShuiGuResult.success(routerVOList);
    }

    /**
     * 查询所有的菜单
     */
    @GetMapping("list")
    public ShuiGuResult list(UmsMenuParamDto menuParamDto) {
        PageResult<UmsMenu> pageResult = menuService.selectList(menuParamDto);
        return ShuiGuResult.success(pageResult);
    }

    /**
     * 根据id查询详情
     * @return
     */
    @GetMapping("{id}")
    public ShuiGuResult searchInfo(@PathVariable("id") Long id) {
        UmsMenu menu = menuService.searchInfo(id);
        return ShuiGuResult.success(menu);
    }

    /**
     * 删除数据
     * @param ids
     * @return
     */
    @DeleteMapping
    public ShuiGuResult removeMenu(@RequestBody Long[] ids) {
        int row = menuService.removeMenu(ids);
        if(row > 0) {
            return ShuiGuResult.success();
        }
        return ShuiGuResult.error();
    }

}
